<template>
  <div class="box">
    <div class="img">
      <div :class="flag ? 'img1' : 'img2'" @click="tab" :style="styleImg">
        <img src="../images/pic1.png" alt="" />
        <component
          :is="icon1"
          style="
            width: 15px;
            height: 15px;
            color: white;
            position: absolute;
            top: 20px;
            left: 45px;
          "
        />
        <span
          style="
            color: white;
            position: absolute;
            top: 30px;
            left: 30px;
            margin-top: 5px;
          "
          >{{ textStr }}</span
        >
      </div>
      <div :class="!flag ? 'img1' : 'img2'" @click="tab">
        <img src="../images/pic2.png" alt="" />
        <component
          :is="icon2"
          style="
            width: 15px;
            height: 15px;
            color: white;
            position: absolute;
            top: 20px;
            left: 40px;
          "
        />
        <span
          style="
            color: white;
            position: absolute;
            top: 30px;
            left: 25px;
            margin-top: 5px;
          "
          >{{ textStr2 }}</span
        >
      </div>
    </div>
    <div class="btns">
      <el-button @click="router.go(-1)">返回</el-button>
      <div class="des">
        <span>商品列表</span>
        <i>&gt;</i>
        <span>编辑详情</span>
      </div>
      <div class="name">
        <img
          :src="useSum.goodsList[Number(route.query.index)].productimg"
          alt=""
        />
        <span
          >{{useSum.goodsList[Number(route.query.index)].productname}}</span
        >
      </div>
      <el-button>保存</el-button>
      <el-button @click="router.go(-1)" style="background: rgb(153, 153, 153)"
        >取消</el-button
      >
    </div>
    <div class="content" v-show="flag">
      <ul>
        <li style="padding-left: 30px">
          <span>商品ID:</span><i>{{useSum.goodsList[Number(route.query.index)].id}}</i>
        </li>
        <li style="padding-left: 20px"><span>商品分类:</span><i>旅行箱</i></li>
        <li style="padding-left: 20px">
          <span>商品名称:</span
          ><i>{{useSum.goodsList[Number(route.query.index)].productname}}</i>
        </li>
        <li style="padding-left: 10px">
          <i style="color: red">*</i><span>自定义名称:</span
          ><el-input
            v-model="name"
            style="width: 400px"
            placeholder="请输入想编辑的商品名称"
          ></el-input>
        </li>
        <li style="padding-left: 20px">
          <i style="color: red">*</i
          ><span style="margin-right: 10px">分类管理:</span
          ><el-select
            v-model="catOne"
            class="m-2"
            placeholder="请选择分类"
            size="default"
            style="margin-left: 20px; width: 160px"
          >
            <el-option value="男装" label="男装">男装</el-option>
            <el-option value="女装" label="女装">女装</el-option>
          </el-select>
        </li>
      </ul>

      <div class="tabl">
        <h3>商品spu信息</h3>
        <table>
          <thead>
            <tr style="height: 30px">
              <td>主图</td>
              <td>在售价</td>
              <td>月销量</td>
              <td>库存</td>
            </tr>
          </thead>
          <tbody>
            <tr>
              <img
                :src="useSum.goodsList[Number(route.query.index)].productimg"
                alt=""
              />
              <td>9.98元</td>
              <td>10件</td>
              <td>10件</td>
            </tr>
          </tbody>
        </table>
        <h3 style="margin-top: 40px">商品sku信息</h3>
        <table>
          <thead>
            <tr style="height: 30px">
              <td>主图</td>
              <td>在售价</td>
              <td>月销量</td>
              <td>库存</td>
            </tr>
          </thead>
          <tbody>
            <tr>
              <img
                :src="useSum.goodsList[Number(route.query.index)].productimg"
                alt=""
              />
              <td>9.98元</td>
              <td>10件</td>
              <td>10件</td>
            </tr>
            <tr>
              <img
                :src="useSum.goodsList[Number(route.query.index)].productimg"
                alt=""
              />
              <td>9.98元</td>
              <td>10件</td>
              <td>10件</td>
            </tr>
            <tr>
              <img
                :src="useSum.goodsList[Number(route.query.index)].productimg"
                alt=""
              />
              <td>9.98元</td>
              <td>10件</td>
              <td>10件</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <div class="content2" v-show="!flag">
      <ul>
        <li style="margin-left: 60px">
          <span>商品主图:</span>
          <img
            src="https://img0.baidu.com/it/u=1939298539,1971726674&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
            alt=""
          />
        </li>
        <li style="margin-left: 46px">
          <span>商品轮播图:</span>
          <img
            src="https://img0.baidu.com/it/u=1939298539,1971726674&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
            alt=""
          />
          <img
            src="https://img0.baidu.com/it/u=1939298539,1971726674&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
            alt=""
          />
          <img
            src="https://img0.baidu.com/it/u=1939298539,1971726674&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
            alt=""
          />
          <img
            src="https://img0.baidu.com/it/u=1939298539,1971726674&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
            alt=""
          />
          <div style="width: 200px; height: 116px">
            <el-upload
              class="upload-demo"
              drag
              action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
              multiple
            >
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            </el-upload>
          </div>
        </li>
        <li style="margin-left: 50px">
          <span>商品详情页:</span>
          <div>
            <div style="border: 1px solid #ccc;width: 800px;height: 100px;">
              <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editorRef"
                :defaultConfig="toolbarConfig"
              />
              <Editor
                style="height: 500px; overflow-y: hidden"
                v-model="valueHtml"
                :defaultConfig="editorConfig"
                @onCreated="handleCreated"
              />
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup>
import { ref, onBeforeUnmount, shallowRef, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStoreSum } from "@/stores/sum.js";
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { getGoodsList, getGoodsId, getGoodsPrice } from "@/api/goodsData.js";

const useSum = useStoreSum();

const route = useRoute();
const router = useRouter();

const catOne = ref("");

//分类名称
const name = ref("");

//控制切换显示
const flag = ref(true);

const styleImg = ref("top: -10px;");

const textStr = ref("基本信息");
const textStr2 = ref("图片编辑");

const icon1 = ref("Monitor");
const icon2 = ref("PictureFilled");

const editorRef = shallowRef();

// 内容 HTML
const valueHtml = ref("<p>hello</p>");

// 模拟 ajax 异步获取内容
onMounted(() => {
  setTimeout(() => {
    valueHtml.value = "<p>模拟 Ajax 异步设置内容</p>";
  }, 1500);
  // getGoodsData();
});


// getGoodsData();
// async function getGoodsData() {
//   const res = await getGoodsList();
//   useSum.goodsList = res.data.items;
//   console.log(useSum.goodsList);
//   window.localStorage.goodsList = JSON.stringify(useSum.goodsList);
// }

const toolbarConfig = {};
const editorConfig = { placeholder: "请输入内容..." };

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

function tab() {
  flag.value = !flag.value;
  if (flag.value) {
    textStr.value = "基本信息";
    textStr2.value = "图片编辑";
    icon1.value = "Monitor";
    icon2.value = "PictureFilled";
  } else {
    textStr.value = "图片编辑";
    textStr2.value = "基本信息";
    icon1.value = "PictureFilled";
    icon2.value = "Monitor";
  }
}
</script>

<style lang="less" scoped>
.box {
  width: 1320px;
  height: 1200px;
  margin-top: 20px;
  position: relative;
  background: rgb(241, 245, 251);
  .img {
    display: flex;
    position: absolute;
    top: 50px;
    left: 60px;
    div {
      cursor: pointer;
    }
    .img1 {
      position: absolute;
    }

    .img2 {
      position: absolute;
      left: 90px;
    }
    img {
      width: 100px;
    }
  }
  .btns {
    padding-top: 10px;
    margin-left: 10px;
    display: flex;
    .el-button {
      background: rgb(23, 28, 73);
      color: white;
    }
    .des {
      color: rgb(23, 28, 73);
      margin-left: 10px;
      width: 160px;
      height: 32px;
      background: white;
      box-shadow: 0 10px 20px rgb(0 0 0 / 20%);
      display: flex;
      align-items: center;
      justify-content: center;
      i {
        margin-left: 10px;
        margin-right: 10px;
      }
    }
    .name {
      width: 440px;
      height: 32px;
      box-shadow: 0 10px 20px rgb(0 0 0 / 20%);
      background: white;
      margin-left: 15px;
      display: flex;
      align-items: center;
      margin-right: 10px;
      img {
        width: 32px;
        height: 32px;
        margin-right: 20px;
      }
    }
  }

  .content {
    width: 1300px;
    margin: 20px auto;
    height: 800px;
    box-shadow: 0 10px 20px rgb(0 0 0 / 20%);
    background: white;
    ul {
      padding-top: 80px;
      margin-left: 20px;
      li {
        margin-bottom: 30px;
        span {
          margin-right: 30px;
          font-weight: 600;
        }
      }
    }
    .tabl {
      margin-left: 30px;

      h3 {
        height: 30px;
        padding-left: 10px;
        line-height: 30px;
        font-size: 12px;
        font-weight: 600;
        background: rgb(242, 242, 242);
      }
      img {
        width: 32px;
        height: 32px;
        margin-right: 90px;
      }
      table {
        width: 100%;
        height: 100%;
        padding-left: 30px;
        padding-top: 30px;
        border-left: 1px solid rgb(242, 242, 242);
        border-bottom: 1px solid rgb(242, 242, 242);
        thead {
          tr {
            display: flex;
            justify-content: flex-start;
            td {
              margin-right: 100px;
            }
          }
        }
        tbody {
          tr {
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: flex-start;
            td {
              margin-right: 100px;
            }
          }
        }
      }
    }
  }

  .content2 {
    width: 1300px;
    margin: 20px auto;

    padding-top: 100px;
    height: 1000px;
    box-shadow: 0 10px 20px rgb(0 0 0 / 20%);
    background: white;
    ul {
      li {
        display: flex;
        margin-bottom: 80px;
        span {
          margin-right: 20px;
          font-size: 14px;
          font-weight: 600;
        }
        img {
          width: 116px;
          height: 112px;
          margin-right: 20px;
        }
      }
    }
  }
}
</style>
